const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');

const mine = {
	"css": "text/css",
	"gif": "image/gif",
	"html": "text/html",
	"ico": "image/x-icon",
	"jpeg": "image/jpeg",
	"jpg": "image/jpeg",
	"js": "text/javascript",
	"json": "application/json",
	"pdf": "application/pdf",
	"png": "image/png",
	"svg": "image/svg+xml",
	"swf": "application/x-shockwave-flash",
	"tiff": "image/tiff",
	"txt": "text/plain",
	"wav": "audio/x-wav",
	"wma": "audio/x-ms-wma",
	"wmv": "video/x-ms-wmv",
	"xml": "text/xml"
}

http.createServer(function (request, response) {

	let pathname = url.parse(request.url).pathname === '/' ? 'index.html' : url.parse(request.url).pathname;
  let realPath = path.join("./../", pathname);    //这里设置自己的文件名称;

  let ext = path.extname(realPath);
  ext = ext ? ext.slice(1) : 'unknown';
	fs.readFile(realPath, function (err, file) {
		if (err) {
			response.writeHead(500, {
				'Content-Type': 'text/plain'
			});
			response.end(err);
		} else {
			let contentType = mine[ext] || "text/plain";
			response.writeHead(200, {
				'Content-Type': contentType
			});
			response.write(file);
			response.end();
		}
	});


}).listen(8888);

// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');